package mulegame;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;

/**
 * Displays score of all players and updates them during gameplay
 * @author AREA 29
 *
 */
public class Scores extends JPanel{
    private Player[] players;
    private JLabel[] names;
    private JLabel[] scores;
    /**
     * displays the score of all the players and updates them as they change
     * @param players array of players 
     */
    public Scores(Player[] players){
        super();
        setLayout(new GridLayout(players.length*2, 1));
        applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		setBounds(20, 116, 193, 262);
		names = new JLabel[players.length];
		scores = new JLabel[players.length];
		for(int i = 0; i < players.length; i++){
		    names[i] = new JLabel();
		    names[i].setHorizontalAlignment(SwingConstants.LEFT);
            names[i].setForeground(Color.BLACK);
            names[i].setFont(new Font("MV Boli", Font.BOLD, 24));
            add(names[i]);
            
            scores[i] = new JLabel();
            scores[i].setHorizontalAlignment(SwingConstants.RIGHT);
            scores[i].setForeground(Color.BLACK);
            scores[i].setFont(new Font("MV Boli", Font.BOLD, 24));
            add(scores[i]);
		}
        this.players = players;
        update();
    }
     /**
      * updates scores of all players    
      */
    public void update(){
        Logger.println("Scores update called");
        removeAll();
        JLabel currName, currScore;
        for(int i = 0; i < players.length; i++){
            //update name label
            names[i].setText(players[i].getName());
            names[i].setBorder(new LineBorder(players[i].getColor(), 2));
            add(names[i]);
            
            //update score label
            scores[i].setText(Integer.toString(players[i].getScore()));
            add(scores[i]);
        }
    }
        
}
